package com.example.bluetoothtest.db.dao;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.example.bluetoothtest.db.UnderCoverDBHelper;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class UnderCoverDao
{
	private Context context;
	private UnderCoverDBHelper dbHelper;
	public UnderCoverDao(Context context, UnderCoverDBHelper dbHelper)
	{
		super();
		this.context = context;
		this.dbHelper = dbHelper;
	}
	public void addIdentity(String others,String undercover,String type ){
		if(find(others,undercover)){
			return ;
		}
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if(db.isOpen()){
			db.execSQL("insert into undercover  values (null,?,?,?)", new Object[]{others,undercover,type});
			db.close();
		}
	}
	public boolean find(String others, String undercover)
	{
		boolean result = false;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		if (db.isOpen()) {
			Cursor cursor = db.rawQuery(
					"select _id from undercover where others=? and spy=?",
					new String[] { others,undercover });
			if (cursor.moveToNext()) {
				result = true;
			}
			cursor.close();

			db.close();
		}
		return result;
		
	}
	
	/**
	 * 
	 * @return string[0]  Ϊothers   string[1]  Ϊspy
	 */
	public String getRandomAdventure()
	{
		String result=new String();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		if (db.isOpen()) {
			Cursor cursor = db.rawQuery(
					"SELECT * FROM adventures ORDER BY RANDOM() limit 1",
					null);//ROUND(UnitPrice,0)
			if (cursor.moveToNext()) {
				result =cursor.getString(1);
			}
			cursor.close();
			db.close();
		}
		return result;
	}
	public String getRandomTruewords()
	{
		String result=new String();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		if (db.isOpen()) {
			Cursor cursor = db.rawQuery(
					"SELECT * FROM truewords ORDER BY RANDOM() limit 1",
					null);//ROUND(UnitPrice,0)
			if (cursor.moveToNext()) {
				result =cursor.getString(1);
			}
			cursor.close();
			db.close();
		}
		return result;
	}
	public String[] getRandomIdenityWord()
	{
		String[] result=new String[2];
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		if (db.isOpen()) {
			Cursor cursor = db.rawQuery(
					"SELECT * FROM undercover ORDER BY RANDOM() limit 1",
					null);//ROUND(UnitPrice,0)
			if (cursor.moveToNext()) {
				result[0] =cursor.getString(1);
				result[1]  =cursor.getString(2);
			}
			cursor.close();
			db.close();
		}
		return result;
	}
	public void addAdventure(String adventure)
	{
		if(find(adventure)){
			return ;
		}
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if(db.isOpen()){
			db.execSQL("insert into adventures  values (null,?,null,null)", new Object[]{adventure});
			db.close();
		}
	}
	public void addTruewords(String trueword)
	{
		//if find()
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		if(db.isOpen()){
			db.execSQL("insert into truewords  values (null,?,null,null)", new Object[]{trueword});
			db.close();
		}
	}
	private boolean find(String adventure)
	{
		boolean result = false;
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		if (db.isOpen()) {
			Cursor cursor = db.rawQuery(
					"select _id from adventures where content=? ",
					new String[] { adventure});
			if (cursor.moveToNext()) {
				result = true;
			}
			cursor.close();

			db.close();
		}
		return result;
		
	}
	
}
